<%--
  Created by IntelliJ IDEA.
  User: bin.liu
  Date: 2019/7/1
  Time: 14:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="/pages/sysIndex/default.jsp"/>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-trophy"></i> 审批管理
            </div>
            <div class="box-tools pull-right">
                <sec:authorize
                    access="hasAnyRole({'ROLE_ENTERPRISE_TASK_ORDER_AUDIT'})">
                    <a href="#" class="btn btn-default" id="btnSearch" title="查询"><i class="fa fa-filter"></i> 查询</a>
                    <a href="javascript:;" onclick="audit(true)"
                        class="btn btn-default btnTool" title="审核通过"><i class="fa fa-plus"></i> 审核通过</a>
                    <a href="javascript:;" onclick="audit(false)"
                        class="btn btn-default btnTool" title="审核驳回"><i class="fa fa-plus"></i> 审核驳回</a>
                </sec:authorize>
            </div>
        </div>

        <div class="box-body">
            <form id="searchForm" action="<c:url value="/web/admin/taskOrderAudit/listJson.action"/>" method="post"
                  class="form-inline hide" data-page-no="1" data-page-size="20" data-order-by="">
                <div class="form-group">
                    <label class="control-label">流水号：</label>
                    <div class="control-inline">
                        <input type="text" id="searchCode" name="searchCode" value="" maxlength="64"
                               class="form-control width-200">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">任务内容：</label>
                    <div class="control-inline">
                        <input type="text" id="taskContent" name="taskContent" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label">发&nbsp;&nbsp;布&nbsp;&nbsp;人：</label>
                    <div class="control-inline">
                        <input type="text" id="reportUser" name="reportUser" value="" maxlength="64"
                               style="width: 200px"
                               class="form-control">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label">开始时间：</label>
                    <div class="control-inline">
                        <input type="text" id="startTime" name="startTime" value="" maxlength="64"
                               class="form-control Wdate " style="width: 200px" autoComplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">结束时间：</label>
                    <div class="control-inline">
                        <input type="text" id="endTime" name="endTime" value="" maxlength="64"
                               class="form-control Wdate " style="width: 200px" autoComplete="off">
                    </div>
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-sm">查询</button>
                    <button type="reset" class="btn btn-default btn-sm">重置</button>
                </div>
            </form>

            <div style="overflow-x:auto">
                <table id="dataGrid"></table>
            </div>
            <div id="dataGridPage"></div>
        </div>
    </div>
</div>
<script>
    $('#dataGrid').dataGrid({
        searchForm: $("#searchForm"),
        showCheckbox: true,
        shrinkToFit: false,
        autoScroll: true,
        columnModel: [
            {header: '工单流水号', name: 'serial', index: 'serial', width: 150, align: "center"},
            {header: '工单类型', name: 'orderTypeName', index: 'orderTypeName', width: 150, align: "center",sortable: false},
            {header: '工单状态', name: 'orderStatusName', index: 'orderStatusName', width: 150, align: "center",sortable: false},
            {header: '工单名称', name: 'orderName', index: 'orderName', width: 150, align: "center"},
            {header: '任务内容', name: 'taskContent', index: 'taskContent', width: 300, align: "center"},
            {header: '任务地址', name: 'taskAddress', index: 'taskAddress', width: 300, align: "center"},
            {header: '发布人', name: 'createUser', index: 'createUser', width: 150, align: "center"},
            {header: '发布时间', name: 'createDate', index: 'createDate', width: 150, align: "center"},
            {header: '当前审核等级', name: 'auditLevel', index: 'auditLevel', width: 150, align: "center"},
            {header: '审批流等级', name: 'level', index: 'level', width: 150, align: "center"},
            {header: '接单人', name: 'receiveUser', index: 'receiveUser', width: 150, align: "center",sortable: false},
            {header: '审批层数', name: 'flowSize', index: 'flowSize', width: 0, align: "center",hidden:true}// hidden:true 不显示该列
        ],

        // 加载成功后执行事件
        ajaxSuccess: function (data) {

        },
    });

    var date = new Date();
    $("#startTime").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        language:'zh-CN',
        minuteStep:1,
        autoclose:true,//选中关闭
        todayBtn: true//今日按钮
    });
    $("#endTime").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        language:'zh-CN',
        minuteStep:1,
        autoclose:true,//选中关闭
        todayBtn: true//今日按钮
    });

    // 时间选择器互动（结束时间不得早于开始时间）
    $("#startTime").on('changeDate', function(ev) {
        $("#endTime").datetimepicker('setStartDate', ev.date);
        var d = $("#endTime").val();
        if (d) {
            var date = new Date(d.replace(/-/g, '/'));
            if (date != 'Invalid Date' && date < ev.date) {
                $("#endTime").datetimepicker('setDate', ev.date)
            }
        }
    });


    function audit(type) {

        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids == null || ids.length == 0) {
            js.showMessage("请选择要审核的工单");
            return false;
        }
        if (ids.length != 1){
            js.showMessage("最多选择一条工单审核");
            return false;
        }
        var id = ids[0];
        if(!type){
            //审批退回
            vLayuiUtil.openLayerWindow('wAudit', '<c:url value="/web/admin/taskOrderAudit/auditOrderEdit.action?id="/>' + id, '审核工单')
        }else{
            js.confirm('确认审批通过？', function () {
                var ret = $("#dataGrid").jqGrid('getRowData', id);
                // console.log(ret.auditLevel,ret.level)
                console.log(ret);
                if(ret.flowSize == 0 || ret.flowSize == 1) {
                    vLayuiUtil.openLayerIframWindow('wJudge', '<c:url value="/web/admin/taskOrderAudit/judgeOrder.action?id="/>' + id, '审核工单',['50%','50%'])
                } else if(Number(ret.auditLevel)+Number(1)==ret.level ){
                    // 终审通过
                    vLayuiUtil.openLayerIframWindow('wJudge', '<c:url value="/web/admin/taskOrderAudit/judgeOrder.action?id="/>' + id, '审核工单',['50%','50%'])
                }
                else{
                    $.ajax({
                        type: 'post',
                        url: "<c:url value="/web/admin/taskOrderAudit/audit.action" />",
                        data: {"id": id, "type": type},
                        dataType: 'json',
                        async: false,
                        success: function (data) {
                            js.showMessage(data.msg);
                            $('#dataGrid').dataGrid('refresh');
                        },
                        error: function () {
                            js.showMessage("出错了，请联系管理员");
                        }
                    });
                }
            })

        }

    }

</script>
